<template>
  <div class="container">
    <el-card class="box-card">
      <el-row>
        <el-col :span="24">
          <div class="login-text">账户密码登录</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="form" label-width="0px" :rules="rules">
            <el-form-item prop="name">
              <el-input prefix-icon="el-icon-user" v-model="form.name" placeholder="请输入邮箱"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input prefix-icon="el-icon-s-tools" v-model="form.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">登录</el-button>
              <el-button type="primary" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-divider content-position="center">其它登录方式</el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-image style="width: 40%" :src="require('../assets/qq.png')"></el-image>
          <div class="login-mode">QQ登录</div>
        </el-col>
        <el-col :span="8">
          <el-image style="width: 40%" :src="require('../assets/phone.png')"></el-image>
          <div class="login-mode">手机登录</div>
        </el-col>
        <el-col :span="8">
          <el-image style="width: 40%" :src="require('../assets/wechat.png')"></el-image>
          <div class="login-mode">微信登录</div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    data() {
      return {
        form: {
          name: '',
          password: ''
        },
        //校验规则
        rules: {
          name: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 5,
              message: '长度在 3 到 5 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 5,
              message: '长度在 3 到 5 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      submitForm() {
        //获取form标签对象
        console.log(this) //当前组件对象
        /* this.$refs.form.validate(async function(valid) {
          if (!valid) return
          console.log(this) //undefined
        }) */

        this.$refs.form.validate(async valid => {
          if (!valid) return
          //箭头函数内的this不存在时会向外找
          // console.log(this) //当前组件对象
          let {
            data: res
          } = await axios.post('/login', this.form);

          if (JSON.parse(res).code != 200) {
            return this.$message.error('用户名或密码错误');
          } else {
            localStorage.setItem("apiKey", "abc");
            sessionStorage.setItem("nickname", JSON.parse(res).result.nickname);
            this.$router.push('/home');
          }
        })
      },
      resetForm() {
        this.$refs.form.resetFields();
      }
    }
  }
</script>

<style scoped>
  .container {
    background-image: url(../assets/background.jpg);
    background-repeat: no-repeat;
    background-size: cover;
    height: 100%;
  }

  .el-card {
    width: 27%;
    position: absolute;
    left: 60%;
    top: 16%;
  }

  .login-text {
    font-size: 22px;
    font-weight: 700;
    margin: 10px;
    text-align: center;
  }

  .login-mode {
    font-size: 12px;
    padding: 10px 0px;
  }
</style>
